#! /bin/bash

show_help(){
    echo
    echo " -----------------------------------------------"
    echo "NAME"
    echo "		create_customize_machine.sh"
    echo
    echo "SYNOPSIS"
    echo "		create_customize_machine.sh [OPTION]... --docker-network-name=<name> --network-mask=<network> --docker-machine-name=<name> --ip-address=<IP> --docker-image-name=<name>"
    echo
    echo "DESRIPTION"
    echo "		Create a machine and a new network of docker if necessary. "
    echo "		Also, the password for the root user of the device will be set to root."
    echo
    echo "		--publish, -p"
    echo "			publish a container's port(s) to the host."
    echo
    echo "		--docker-network-name, -dnn"
    echo "			docker network name."
    echo
    echo "		--network-mask, -nm"
    echo "			network."
    echo
    echo "		--docker-machine-name, -dmn"
    echo "			adaptive application MACHINE name."
    echo
    echo "		--ip-address, -ia"
    echo "			adaptive application MACHINE IP Address."
    echo
    echo "		--docker-image-name, -din"
    echo "			docker image name."
    echo
    echo "EXAMPLE"
    echo "		create_customize_machine.sh -dnn=someipdemos -nm=192.168.111.0/24 -dmn=aa_machine -ia=192.168.111.19 -din=vrte_for_ubuntu"
    echo
    echo " -----------------------------------------------"
    exit 0
}

show_invalid_option(){
    echo ""
    echo "Invalid option!"
    show_help
}

PUBLISH_LIST=""
# parse options
for i in $*; do
    case "$i" in
        -dnn=* | --docker-network-name=*)
            DOCKER_NETWORK_NAME=${i#-*=}
            ;;
        -nm=* | --network-mask=*)
            SUBNET=${i#-*=}
            ;;
        -dmn=* | --docker-machine-name=*)
            MACHINE_NAME=${i#-*=}
            ;;
        -ia=* | --ip-address=*)
            IP_ADDRESS=${i#-*=}
            ;;
        -din=* | --docker-image-name=*)
            DOCKER_IMAGE=${i#-*=}
            ;;
        -p=* | --publish=*)
            PUBLISH_LIST+="--publish ${i#-*=} "
            ;;
        -h | --help)
            show_help
            ;;
        *)
            echo "unknown option '$i'"
            show_invalid_option
            usage 1
            ;;
    esac
done

if [ -z "$DOCKER_NETWORK_NAME" ]; then
    echo "the following arguments are required: -dnn/--docker-network-name"
    exit 0
fi

if [ -z "$MACHINE_NAME" ]; then
    echo "the following arguments are required: -dmn/--docker-machine-name"
    exit 0
fi

if [ -z "$IP_ADDRESS" ]; then
    echo "the following arguments are required: -ia/--ip-address"
    exit 0
fi

if [ -z "$DOCKER_IMAGE" ]; then
    echo "the following arguments are required: -din/--docker-image-name"
    exit 0
fi

# create docker network
NETRWORK_EXIST=`docker network ls | grep $DOCKER_NETWORK_NAME`
if [ -z "$NETRWORK_EXIST" ]; then
    if [ -z "$SUBNET" ]; then
        echo "the following arguments are required if $DOCKER_NETWORK_NAME dose not exist: -nm/--network-mask"
        exit 0
    else
        echo $"[INFO] Creating docker network $DOCKER_NETWORK_NAME." 
        docker network inspect $DOCKER_NETWORK_NAME >/dev/null 2>&1 || docker network create --subnet=$SUBNET $DOCKER_NETWORK_NAME
        echo "[INFO] existing docker networks:"
        docker network ls
    fi
fi

echo "[INFO] Docker image to be used for containers: " $DOCKER_IMAGE
echo "[INFO] Creating detached docker container as MACHINE ${MACHINE_NAME}"

echo "[INFO] Creating reating MACHINE as follows."
echo "           --name $MACHINE_NAME"
echo "           ${PUBLISH_LIST}"
echo "           --network=$DOCKER_NETWORK_NAME"
echo "           --ip $IP_ADDRESS"
echo "           --security-opt seccomp=unconfined"
echo "           --cap-add sys_nice"
echo "           --privileged"
echo "           --shm-size 8G"
echo "           --ulimit msgqueue=50000000:50000000"
echo "           --sysctl fs.mqueue.msgsize_max=131072"
echo "           -dit $DOCKER_IMAGE"

# create machine
docker run --name $MACHINE_NAME \
           ${PUBLISH_LIST} \
           --network=$DOCKER_NETWORK_NAME \
           --ip $IP_ADDRESS \
           --security-opt seccomp=unconfined \
           --cap-add sys_nice \
           --privileged \
           --shm-size 8G \
           --ulimit msgqueue=50000000:50000000 \
           --sysctl fs.mqueue.msgsize_max=131072 \
           -dit $DOCKER_IMAGE

docker cp ./setup_ssh.sh $MACHINE_NAME:/
docker exec $MACHINE_NAME /setup_ssh.sh

